namespace SimpleWebServices.Configuration
{
    using System;
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Mvc;
    using Xunit;

    public sealed class SubmissionProcessorSettingFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SubmissionProcessorSetting>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("processor")
                .Implements<IProcessSubmission>()
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            SubmissionProcessorSetting actual = "<processor id='test' />".Deserialize<SubmissionProcessorSetting>();

            Assert.Equal<string>("test", actual.Id);
        }

        [Fact]
        public void deserializeEmpty()
        {
            SubmissionProcessorSetting actual = "<processor />".Deserialize<SubmissionProcessorSetting>();

            Assert.Null(actual.Id);
        }

        [Fact]
        public void serialize()
        {
            SubmissionProcessorSetting obj = new SubmissionProcessorSetting
            {
                Id = "test"
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/processor[@id='test'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SubmissionProcessorSetting());
        }

        [Fact]
        public void prop_Id()
        {
            Assert.True(new PropertyExpectations(typeof(SubmissionProcessorSetting).GetProperty("Id"))
                .XmlAttribute("id")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Process_FormSubmissionNull()
        {
            Assert.Throws<ArgumentNullException>(() => new SubmissionProcessorSetting().Process(null as FormSubmission));
        }

        [Fact]
        public void op_Process_FormSubmission()
        {
            ServiceLocator.Clear();

            try
            {
                ServiceLocator.Register<IProcessSubmission, TestIProcessSubmission>();

                Assert.IsAssignableFrom<SeeOtherResult>(new SubmissionProcessorSetting().Process(new FormSubmission()));
            }
            finally
            {
                ServiceLocator.Reset();
            }
        }

        [Fact]
        public void op_Process_FormSubmission_whenId()
        {
            ServiceLocator.Clear();

            try
            {
                string id = "component";

                ServiceLocator.Register<IProcessSubmission, TestIProcessSubmission>(id);

                SubmissionProcessorSetting obj = new SubmissionProcessorSetting
                {
                    Id = id
                };

                Assert.IsAssignableFrom<SeeOtherResult>(obj.Process(new FormSubmission()));
            }
            finally
            {
                ServiceLocator.Reset();
            }
        }
    }
}